///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.data.vo {
	import flash.utils.describeType;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	
	import sugarcookie.collections.EnhancedArrayCollection;
	import sugarcookie.data.vo.Group;
	
	dynamic public class View extends Proxy {
		private var _id:String;
		private var _components:EnhancedArrayCollection;
		private var _groups:EnhancedArrayCollection;
		
		public function View(id:String) {
			_id = id;
			_components = new EnhancedArrayCollection();
			_groups = new EnhancedArrayCollection();
		}
		
		public function get id():String {
			return _id;
		}
		
		public function get groups():Array {
			return _groups.source;
		}
		
		public function get components():Array {
			return _components.source;	
		}
		
		override flash_proxy function setProperty(name:*, value:*):void {
			var str:String = XML(describeType(value)).@name;
			if (str.indexOf("Component")!=-1) _components.addItemWithKey(name,value);
			else if (str.indexOf("Group")!=-1) _groups.addItemWithKey(name,value);
		}
		
		override flash_proxy function getProperty(name:*):* {
			if (_groups.containsKey(name)) {
	        	var grp:Group = Group(_groups.getItemAtKey(name));
	        	if (grp.type=="array") return grp.components;
	        	else return grp; 
	        } else if (_components.containsKey(name)) {
	        	return _components.getItemAtKey(name);
	        }
	        return null;
		}
	}
}